<?php

 /**
  * @package JooS
  * @subpackage PHPackager
  */

  require_once "JooS/PHPackager/DOM/Element.php";

  class JooS_PHPackager_DOM_Element_Tasks extends JooS_PHPackager_DOM_Element {
    protected $_pi = array();

    /**
     * @return string
     */
    public function getCompiledCode() {
      $compiledCode = array();

      if (sizeof($this->_pi)) {
        $compiledCode[] = "switch (\$stage) {";

        foreach ($this->_pi as $pi) {
          /* @var $pi JooS_PHPackager_DOM_ProcessingInstruction_Build */

          $pi_PHPCode = trim($pi->getCompiledCode());
          if ($pi_PHPCode) {
            $compiledCode[] = "case '" . $pi->getNode()->target . "':";
            if ($pi_PHPCode) {
              $compiledCode[] = $pi_PHPCode;
            }
            $compiledCode[] = "break;";
          }
        }
        $compiledCode[] = "}";
      }
      return implode(" ", $compiledCode);
    }

    /**
     * @return JooS_PHPackager_DOM_Element_Tasks
     */
    public function make() {
      $this->_pi = $this->query("processing-instruction()", "JooS_PHPackager_DOM_ProcessingInstruction_Build");
      foreach ($this->_pi as $pi) {
        /* @var $pi JooS_PHPackager_DOM_ProcessingInstruction_Build */
        $pi->make();
      }

      return $this;
    }

    /**
     * @return JooS_PHPackager_DOM_Element_Tasks
     */
    public function install() {
      require_once "JooS/Event/Phpackager/Package/Method/Create.php";
      require_once "JooS/CodeGenerator/Parameter.php";
      require_once "JooS/CodeGenerator/Method.php";
      require_once "JooS/PHPackager/DOM/Element/Package.php";

      $name = "_" . strtolower($this->getNode()->tagName);

      $newMethod = JooS_CodeGenerator_Method::newInstance( $name )
        ->setVisibility( JooS_CodeGenerator_Method::VISIBILITY_PUBLIC )
        ->setBody( $this->getCompiledCode() )
        ->addParameter( JooS_CodeGenerator_Parameter::newInstance("stage") );
      
      JooS_Event_Phpackager_Package_Method_Create::getInstance()
        ->setMethod($newMethod)
        ->notify();

      return $this;
    }
  }
